package com.caotv.wkreport.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.caotv.wkreport.dao.Bookingroom;
import com.caotv.wkreport.dao.Rooms;
import com.caotv.wkreport.dao.User;
import com.caotv.wkreport.dbutils.BookingRoomTableTool;
import com.caotv.wkreport.dbutils.RoomsTableTool;
import com.caotv.wkreport.utils.BookingMatrix;
import com.caotv.wkreport.utils.CommonUtils;
import com.caotv.wkreport.utils.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

public class BookingRoomInfoAction extends ActionSupport {

	private int roomId;
	private Date startDate;
	private Date endDate;
	private List<Rooms> listRooms = new ArrayList<Rooms>();
	private List<Integer> listHours = new ArrayList<Integer>();
	private List<Integer> listMins = new ArrayList<Integer>();
	private List<Bookingroom> listBookingRoom = new ArrayList<Bookingroom>();
	private List<BookingMatrix> listMatrix = new ArrayList<BookingMatrix>();
	private List<Date> dates = new ArrayList<Date>();

	public List<Date> getDates() {
		return dates;
	}

	public void setDates(List<Date> dates) {
		this.dates = dates;
	}

	public String execute() {
		initListDate();
		initData();
		return "success";
	}
	
	private void initData() {
		System.out.println("initData: roomID: "+ roomId);
		listRooms = RoomsTableTool.getInstance().getAllRooms();
		if (startDate != null) {
//			listBookingRoom = BookingRoomTableTool.getInstance()
//					.getBookingRoomInfoByDate(startDate);
			
			// moi sua
			listBookingRoom = BookingRoomTableTool.getInstance()
					.getBookingRoomInfoByDateStartToEnd(startDate, endDate, roomId);
			
			for(int i = 0;i < listBookingRoom.size();i++) {
				System.out.println("Gia tri dat phong BookID: "+listBookingRoom.get(i).getBookId());				
				System.out.println("Gia tri dat phong Start time: "+listBookingRoom.get(i).getStartTime());
				System.out.println("Gia tri dat phong end time: "+listBookingRoom.get(i).getEndTime());			
			}	
	
//			listMatrix = CommonUtils.fillBookingMatrix(listRooms,
//					listBookingRoom, startDate);
			listMatrix = CommonUtils.fillBookingInfoMatrix(roomId, listBookingRoom, dates);
			
			for(int i = 0;i < listMatrix.size();i++) {
				BookingMatrix book = listMatrix.get(i);
				System.out.println("List Matrix size: "+listMatrix.size());				
				System.out.println("thoi gian: "+book.getTimeZone());
				System.out.println("gia tri book: "+book.getRow()[1]);
			}
			
		}
		for (int i = 9; i < 19; i++) {
			listHours.add(i);
		}
		for (int i = 0; i < 60; i += 30) {
			listMins.add(i);
		}
	}
	
	private void initListDate() {
		long interval = 24*1000 * 60 * 60; // 1 hour in millis
		long endTime =endDate.getTime() ; // create your endtime here, possibly using Calendar or Date
		long curTime = startDate.getTime();
		while (curTime <= endTime) {
		    dates.add(new Date(curTime));
		    curTime += interval;
		}
		for(int i=0;i<dates.size();i++){
		    Date lDate =(Date)dates.get(i);   
		    System.out.println(" Date is ..." + lDate);
		}
	}
/*
	public void validate() {
		initData();
		if (roomId == -1) {
			addFieldError("roomId", getText("errors.roomReq"));
		} else if (StringUtils.isNullOrEmpty(startDate)) {
			addFieldError("startDate", getText("errors.startDateReq"));
		} else if (roomId == 0) {
			addFieldError("roomId", getText("errors.roomReq"));
		} else if (StringUtils.isNullOrEmpty(endDate)) {
			addFieldError("endDate", getText("errors.endDateReq"));
		} else {
			Date today = new Date();
			today = Calendar.getInstance().getTime();
			System.out.print("Today is:" + today.toString());

			startDate.setHours(0);
			startDate.setMinutes(0);
			startDate.setSeconds(0);

			endDate.setHours(0);
			endDate.setMinutes(0);
			endDate.setSeconds(0);

			if (startDate.getTime() != endDate.getTime()) {
				addFieldError("endDate", getText("errors.sameDate"));
			}

			if (startDate.before(today)) {
				addFieldError("startDate", getText("errors.startDateAfter"));
			} else if (endDate.getTime() <= startDate.getTime()) {
				addActionError(getText("errors.endDateAfter"));
			} else {
				if (!CommonUtils.checkBookedRoom(startDate, endDate, roomId)) {
					addActionError(getText("errors.roomBooked"));
				}
			}
		}
	}
*/
	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int roomId) {
		this.roomId = roomId;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}


	public List<Rooms> getListRooms() {
		return listRooms;
	}

	public void setListRooms(List<Rooms> listRooms) {
		this.listRooms = listRooms;
	}

	public List<Bookingroom> getListBookingRoom() {
		return listBookingRoom;
	}

	public void setListBookingRoom(List<Bookingroom> listBookingRoom) {
		this.listBookingRoom = listBookingRoom;
	}



	public List<Integer> getListHours() {
		return listHours;
	}

	public void setListHours(List<Integer> listHours) {
		this.listHours = listHours;
	}

	public List<Integer> getListMins() {
		return listMins;
	}

	public void setListMins(List<Integer> listMins) {
		this.listMins = listMins;
	}

	public List<BookingMatrix> getListMatrix() {
		return listMatrix;
	}

	public void setListMatrix(List<BookingMatrix> listMatrix) {
		this.listMatrix = listMatrix;
	}

}
